home *** CD-ROM | disk | FTP | other *** search
Text File | 1995-11-08 | 12.9 KB | 431 lines | [TEXT/MPS ] |
- //========================================================================================
- //
- // File: HelloPrt.cpp
- // Release Version: $ 1.0d11 $
- //
- // Copyright: © 1995 by Apple Computer, Inc., all rights reserved.
- //
- //========================================================================================
-
- #include "Hello.hpp"
-
- #ifndef HELLOPRT_H
- #include "HelloPrt.h"
- #endif
-
- #ifndef HELLOFRM_H
- #include "HelloFrm.h"
- #endif
-
- #ifndef HELLOSEL_H
- #include "HelloSel.h"
- #endif
-
- // ----- Framework Layer -----
-
- #ifndef FWUTIL_H
- #include "FWUtil.h"
- #endif
-
- #ifndef FWITERS_H
- #include "FWIters.h"
- #endif
-
- #ifndef FWPRESEN_H
- #include "FWPresen.h"
- #endif
-
- // ----- OS Layer -----
-
- #ifndef FWMENU_H
- #include "FWMenu.h"
- #endif
-
- #ifndef FWCFMRES_H
- #include "FWCFMRes.h"
- #endif
-
- #ifndef FWRESTYP_H
- #include "FWResTyp.h"
- #endif
-
- #ifndef FWSUSINK_H
- #include "FWSUSink.h"
- #endif
-
- #ifndef FWEVENT_H
- #include "FWEvent.h"
- #endif
-
- #ifndef FWBARRAY_H
- #include "FWBArray.h"
- #endif
-
- // ----- Foundation Includes -----
-
- #ifndef FWSTREAM_H
- #include "FWStream.h"
- #endif
-
- #ifndef FWSTRS_H
- #include "FWStrs.h"
- #endif
-
- #ifndef FWSTRGAR_H
- #include "FWStrgAr.h"
- #endif
-
- #ifndef FWSUSINK_H
- #include "FWSUSink.h"
- #endif
-
- // ----- OpenDoc Includes -----
-
- #ifndef SOM_Module_OpenDoc_StdProps_defined
- #include <StdProps.xh>
- #endif
-
- #ifndef SOM_ODTranslation_xh
- #include <Translt.xh>
- #endif
-
- #ifndef SOM_ODSession_xh
- #include <ODSessn.xh>
- #endif
-
- //========================================================================================
- // Constants and Globals
- //========================================================================================
-
- const ODValueType CHelloPart::kPartKind = kODFHelloKind;
- const ODValueType CHelloPart::kPartUserName = kODFHelloEditorUserString;
-
- const ODValueType kHelloPartSetting = "Apple:ODFExamples:Setting:ODFHello";
-
- const ODCommandID cFirstCommand = FW_kFirstUserCommandID;
- const ODCommandID cSecondCommand = FW_kFirstUserCommandID + 1;
-
- #define kMainPresentation "ODFExamples:Presentation:ODFHello"
-
- //========================================================================================
- // Runtime info
- //========================================================================================
-
- #ifdef FW_BUILD_MAC
- #pragma segment odfhello
- #endif
-
- //========================================================================================
- // CHelloPart class
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // CHelloPart constructor
- //----------------------------------------------------------------------------------------
-
- CHelloPart::CHelloPart(ODPart* odPart) :
- FW_CPart(odPart, CHelloPart::kPartKind, CHelloPart::kPartUserName, FW_gInstance, kPartIconID),
- fTextData1(""),
- fTextData2(""),
- fIsFirstString(TRUE),
- fPresentation(NULL)
- {
- }
-
- //----------------------------------------------------------------------------------------
- // CHelloPart destructor
- //----------------------------------------------------------------------------------------
-
- CHelloPart::~CHelloPart()
- {
- }
-
- //----------------------------------------------------------------------------------------
- // CHelloPart::Initialize
- //----------------------------------------------------------------------------------------
-
- void CHelloPart::Initialize(Environment* ev) // Override
- {
- FW_CPart::Initialize(ev);
-
- // ----- Register our Presentation
- fPresentation = RegisterPresentation(ev, kMainPresentation, TRUE,
- new CHelloSelection(ev, this));
-
- // ----- Initialize the Archiver component -----
- FW_InitializeArchiving(FW_gInstance);
-
- // ----- Open the resource file for this shared library -----
- FW_CSharedLibraryResourceFile resFile;
-
- // ----- Create the Hello menu -----
- FW_CPullDownMenu* pullDownMenu = new FW_CPullDownMenu(ev, resFile, kHelloPartStrings, kHelloMenuString);
-
- // ----- Add the Hello menu items -----
- pullDownMenu->AppendTextItem(ev, resFile, kHelloPartStrings, kFirstMenuString, cFirstCommand, '1');
- pullDownMenu->AppendTextItem(ev, resFile, kHelloPartStrings, kSecondMenuString, cSecondCommand, '2');
-
- // ----- Add the Hello menu to the menu bar -----
- GetMenuBar(ev)->AdoptMenuLast(ev, pullDownMenu);
-
- // ----- Initialize the text data strings -----
- FW_CString32 platformString;
- ::FW_LoadStringByID(resFile, kHelloPartStrings, MULTISTRINGRES, kFirstString, fTextData1);
- ::FW_LoadStringByID(resFile, kHelloPartStrings, MULTISTRINGRES, kSecondString, fTextData2);
- ::FW_LoadStringByID(resFile, kHelloPartStrings, MULTISTRINGRES, kPlatformString, platformString);
- fTextData2 += platformString;
- }
-
- //------------------------------------------------------------------------------
- // CHelloPart::InternalizeContent
- //------------------------------------------------------------------------------
-
- void CHelloPart::InternalizeContent(Environment* ev,
- ODStorageUnit* storageUnit,
- FW_CCloneInfo* cloneInfo) // Override
- {
- FW_UNUSED(cloneInfo);
-
- if (FW_SUExistsThenFocus(ev, storageUnit, kODPropContents, CHelloPart::kPartKind))
- {
- if (storageUnit->GetSize(ev) != 0)
- {
- // Read the text data strings from storage
- FW_CStorageUnitSink suSink(storageUnit, kODPropContents, CHelloPart::kPartKind);
- FW_CReadableStream archive(&suSink);
-
- unsigned long stringCount;
- archive >> stringCount;
- if (stringCount > 0)
- FW_CStringArchiver::Read(archive, fTextData1);
- if (stringCount > 1)
- FW_CStringArchiver::Read(archive, fTextData2);
- }
- if (storageUnit->Exists(ev, kODPropContents, kHelloPartSetting, 0))
- {
- // Read the part's setting from storage
- storageUnit->Focus(ev, kODPropContents, kODPosUndefined, kHelloPartSetting, 0, kODPosUndefined);
- FW_CByteArray byteArray;
- storageUnit->GetValue(ev, sizeof(FW_Boolean), byteArray);
- byteArray.CopyBuffer(&fIsFirstString, sizeof(FW_Boolean));
- }
- }
- }
-
- //----------------------------------------------------------------------------------------
- // CHelloPart::ExternalizeContent
- //----------------------------------------------------------------------------------------
-
- void CHelloPart::ExternalizeContent(Environment* ev,
- ODStorageUnit* storageUnit,
- FW_CCloneInfo* cloneInfo) // Override
- {
- FW_UNUSED(cloneInfo);
-
- storageUnit->Focus(ev, kODPropContents, kODPosUndefined, CHelloPart::kPartKind, 0, kODPosUndefined);
- storageUnit->Remove(ev);
- storageUnit->AddValue(ev, CHelloPart::kPartKind);
-
- // Write the two text data strings to storage
- FW_CStorageUnitSink suSink(storageUnit, kODPropContents, CHelloPart::kPartKind);
- FW_CWritableStream archive(&suSink);
- unsigned long stringCount = 2;
-
- archive << stringCount;
- FW_CStringArchiver::Write(archive, fTextData1);
- FW_CStringArchiver::Write(archive, fTextData2);
-
- // Write the part's setting to storage
- storageUnit->Focus(ev, kODPropContents, kODPosUndefined, kHelloPartSetting, 0, kODPosUndefined);
- storageUnit->Remove(ev);
- storageUnit->AddValue(ev, kHelloPartSetting);
-
- FW_CByteArray byteArray(&fIsFirstString, sizeof(FW_Boolean));
- storageUnit->SetValue(ev, byteArray);
- }
-
- //----------------------------------------------------------------------------------------
- // CHelloPart::AddProperties
- //----------------------------------------------------------------------------------------
-
- void CHelloPart::AddProperties(Environment *ev, ODStorageUnit* storageUnit) // Override
- {
- FW_CPart::AddProperties(ev, storageUnit);
-
- // FW_SUAddPropValue correctly tests if the property and value exist before
- // trying to create them
- FW_SUAddPropValue(ev, storageUnit, kODPropContents, kHelloPartSetting);
- }
-
- //----------------------------------------------------------------------------------------
- // CHelloPart::NewFrame
- //----------------------------------------------------------------------------------------
-
- FW_CFrame* CHelloPart::NewFrame(Environment* ev,
- ODFrame* odFrame,
- FW_CPresentation* presentation,
- FW_Boolean fromStorage) // Override
- {
- FW_UNUSED(fromStorage);
-
- return new CHelloFrame(ev, odFrame, presentation, this);
- }
-
- //----------------------------------------------------------------------------------------
- // CHelloPart::DoMenu
- //----------------------------------------------------------------------------------------
-
- FW_Boolean CHelloPart::DoMenu(Environment* ev, const FW_CMenuEvent& theMenuEvent) // Override
- {
- FW_Boolean menuHandled = FALSE;
- ODCommandID id = theMenuEvent.GetCommandID(ev);
-
- switch (id)
- {
- case cFirstCommand:
- case cSecondCommand:
- this->DoHelloCommand(ev, id);
- menuHandled = TRUE;
- break;
-
- default:
- menuHandled = FALSE;
- }
-
- return menuHandled;
- }
-
- //----------------------------------------------------------------------------------------
- // CHelloPart::DoAdjustMenus
- //----------------------------------------------------------------------------------------
-
- FW_Boolean CHelloPart::DoAdjustMenus(Environment* ev, FW_CMenuBar* menuBar,
- FW_Boolean hasMenuFocus,
- FW_Boolean isRoot) // Override
- {
- if (hasMenuFocus)
- {
- // Set up the Hello menu
- menuBar->EnableCommand(ev, cFirstCommand, !fIsFirstString);
- menuBar->EnableCommand(ev, cSecondCommand, fIsFirstString);
- }
-
- return FALSE;
- }
-
- //----------------------------------------------------------------------------------------
- // CHelloPart::DoHelloCommand
- //----------------------------------------------------------------------------------------
-
- void CHelloPart::DoHelloCommand(Environment* ev, ODCommandID id)
- {
- // Toggle the flag that tells which string to display
- fIsFirstString = !fIsFirstString;
-
- // Notify the proper authorities
- this->PartChanged(ev);
- }
-
- //----------------------------------------------------------------------------------------
- // CHelloPart::PartChanged
- //----------------------------------------------------------------------------------------
-
- void CHelloPart::PartChanged(Environment* ev)
- {
- // Mark the document's draft as changed so it can be saved
- this->Changed(ev);
-
- // Mark the display frame as changed, so that containing parts can update links
- FW_CPresentationFrameIterator piter(fPresentation);
- this->ContentUpdated(ev, piter.First(), kODUnknownUpdate);
-
- // Force all display frames to be redrawn
- fPresentation->Invalidate(ev);
- }
-
- //----------------------------------------------------------------------------------------
- // CHelloPart::GetResourceString
- //----------------------------------------------------------------------------------------
-
- void CHelloPart::GetResourceString(FW_ResourceId multiStringId,
- FW_ResourceId stringId,
- FW_CString& string)
- {
- // ----- Open the resource file for this shared library -----
- FW_CSharedLibraryResourceFile resFile;
-
- // ----- Read the string from the resource file -----
- ::FW_LoadStringByID(resFile, multiStringId, MULTISTRINGRES, stringId, string);
- }
-
- //----------------------------------------------------------------------------------------
- // CHelloPart::GetTextData
- //----------------------------------------------------------------------------------------
-
- const FW_CString& CHelloPart::GetTextData()
- {
- if (fIsFirstString)
- return fTextData1;
- else
- return fTextData2;
- }
-
- //----------------------------------------------------------------------------------------
- // CHelloPart::ClearTextData
- //----------------------------------------------------------------------------------------
-
- void CHelloPart::ClearTextData()
- {
- FW_CString255 string;
- this->GetResourceString(kHelloPartStrings, kBlankString, string);
- if (fIsFirstString)
- fTextData1 = string;
- else
- fTextData2 = string;
- }
-
- //----------------------------------------------------------------------------------------
- // CHelloPart::SetTextData
- //----------------------------------------------------------------------------------------
-
- void CHelloPart::SetTextData(const FW_CString& newText)
- {
- if (fIsFirstString)
- fTextData1 = newText;
- else
- fTextData2 = newText;
- }
-
- //----------------------------------------------------------------------------------------
- // CHelloPart::GetData
- //----------------------------------------------------------------------------------------
-
- void CHelloPart::GetData(Environment* ev, FW_Boolean& setting, FW_CString& string)
- {
- setting = fIsFirstString;
- string = this->GetTextData();
- }
-
- //----------------------------------------------------------------------------------------
- // CHelloPart::SetData
- //----------------------------------------------------------------------------------------
-
- void CHelloPart::SetData(Environment* ev, FW_Boolean setting, const FW_CString& string)
- {
- fIsFirstString = setting; // "select" the string
- this->SetTextData(string); // set the text
- this->PartChanged(ev); // Notify the proper authorities
- }
-
- //----------------------------------------------------------------------------------------
- // CHelloPart::ClearData
- //----------------------------------------------------------------------------------------
-
- void CHelloPart::ClearData(Environment* ev, FW_Boolean setting)
- {
- fIsFirstString = setting; // "select" the string
- this->ClearTextData(); // then clear it
- this->PartChanged(ev); // Notify the proper authorities
- }
-
-